<?php
class Model_TableCommentaire implements Model_ITable
{
	private static $con = null;
	private static $reqSelectAll = null;
	private static $reqSelectOne = null;
	private static $reqInsert = null;
	private static $reqDeleteOne = null;
	private static $reqUpdate = null;
	
	public static function setConnexion(MyPdo $pdo)
	{
		self::$con = $pdo;
	}
	
	public static function getConnexion()
	{
		return self::$con;
	}

	public static function init()
	{
		self::$reqSelectAll = self::$con->prepare('SELECT * FROM commentaire ORDER BY id_commentaire');
		self::$reqSelectOne = self::$con->prepare('SELECT * FROM commentaire WHERE id_commentaire = :idcond');
		self::$reqDeleteOne = self::$con->prepare('DELETE FROM commentaire WHERE id_commentaire = :idcond');
		self::$reqInsert = self::$con->prepare('INSERT INTO commentaire(contenu, date_commentaire, id_association, id_evenement) VALUES(:contenu, :datecommentaire, :idassociation, :idevenement)');
		self::$reqUpdate = self::$con->prepare('UPDATE commentaire SET contenu = :contenu,
			date_commentaire = :datecommentaire,
			id_association = :idassociation,
			id_evenement = :idevenement WHERE id_commentaire = :idcond
		');
	}

	public static function fetchAll()
	{
		self::$reqSelectAll->execute();
		$results = self::$reqSelectAll->fetchAll();
		return $results;
	}

	public static function fetchOne($id)
	{
		self::$reqSelectOne->execute(array(':idcond' => $id));
		$results = self::$reqSelectOne->fetchAll();
		if( empty($results[0]) )
			return array();
		else
			return $results[0];
	}
	
	public static function delete($id)
	{
		self::$reqDeleteOne->execute(array(':idcond' => $id));
	}
	
	public static function insert($contenu, $datecommentaire, $idassociation, $idevenement)
	{
		self::$reqInsert->execute(array(
			':contenu' => $contenu,
			':datecommentaire' => $datecommentaire,
			':idassociation' => $idassociation,
			':idevenement' => $idevenement
		));
		return self::$con->lastInsertId();
	}
	
	public static function update($id, $contenu, $datecommentaire, $idassociation, $idevenement)
	{
		self::$reqUpdate->execute(array(
			':contenu' => $contenu,
			':datecommentaire' => $datecommentaire,
			':idassociation' => $idassociation,
			':idevenement' => $idevenement,
			':idcond' => $id
		));
	}
}
